package de.dhbw.asa.model;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * This class provides all attributes for a given order
 * @author aar
 *
 */
public class Order extends Database
{
	
	public int iOrderNr;
	public int iCustomerNr;
	public String sComment;
	public int iAdressNr;
	public int iStatusNr;
	
	/*
	 * this method loads a given order
	 */
	public boolean load(int iOrderNr)
	{
		boolean bReturn = false;
		String sOrderNr = String.valueOf(iOrderNr);
		StringBuffer sbQuery = new StringBuffer();
		sbQuery.append("Select * from Order where OrderNr = '");
		sbQuery.append(sOrderNr);
		sbQuery.append("';");
		try
		{
			if (this.createConnection())
			{
				ResultSet jResult = this.executeQuery(sbQuery.toString());
				if (jResult.next())
				{
					this.iOrderNr = iOrderNr;
					this.iCustomerNr = Integer.parseInt(jResult.getString("customerNr"));
					this.sComment = jResult.getString("comment");
					this.iAdressNr = Integer.parseInt(jResult.getString("AdressNr"));
					this.iStatusNr = Integer.parseInt(jResult.getString("StatusNr"));
				}
				bReturn = true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	/*
	 * prepares a list with all the items of an order
	 */
	public List<Item> loadAllItems()
	{
		List<Item> jList = new ArrayList<Item>();
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("Select * from Item where orderNr =");
			sbQuery.append(this.iOrderNr);
			sbQuery.append(";");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			while (jResult.next())
			{
				Item jItem = new Item();
				jItem.load(jResult.getInt("itemNr"));
				jList.add(jItem);
			}
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return jList;
	}
	
	/*
	 * this method saves changes of an order
	 */
	public String saveChanges()
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Insert the Order into the Database
			sbQuery.append("update webshop.order set customernr=");
			sbQuery.append(this.iCustomerNr);
			sbQuery.append(", comment ='");
			sbQuery.append(this.sComment);
			sbQuery.append("', adressnr =");
			sbQuery.append(this.iAdressNr);
			sbQuery.append(", StatusNr =");
			sbQuery.append(this.iStatusNr);
			sbQuery.append(", date=CURDATE()");
			sbQuery.append(" where orderNr =");
			sbQuery.append(this.iOrderNr);
			sbQuery.append(";");
			this.execute(sbQuery.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	/*
	 * this method saves a new order in the database
	 */
	public String saveNewOrder(int iCustomerNr, String sComment, int iAdressNr, int iStatusNr)
	{
		String sReturn = "";
		this.iCustomerNr = iCustomerNr;
		this.sComment = sComment;
		this.iAdressNr = iAdressNr;
		this.iStatusNr = iStatusNr;
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Finding out the last OrderNr of our orders
			sbQuery.append("Select orderNr from webshop.Order;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			int iLastOrderNr = 0;
			while (jResult.next())
			{
				iLastOrderNr = jResult.getInt("OrderNr");
			}
			this.iOrderNr = iLastOrderNr+1;
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the customer into the Database
			sbQuery2.append("insert into webshop.order (orderNr, customerNr, comment, AdressNr, statusNr, date) values (");
			sbQuery2.append(this.iOrderNr);
			sbQuery2.append(",");
			sbQuery2.append(this.iCustomerNr);
			sbQuery2.append(",'");
			sbQuery2.append(this.sComment);
			sbQuery2.append("',");
			sbQuery2.append(this.iAdressNr);
			sbQuery2.append(",");
			sbQuery2.append(this.iStatusNr);
			sbQuery2.append(", CURDATE());");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	public String loadStatus()
	{
		Status jStatus = new Status();
		String sReturn;
		if (jStatus.load(this.iStatusNr))
		{
			sReturn=jStatus.getsStatusName();
		}
		else
		{
			System.err.println("Status konnte nicht geladen werden...");
			sReturn = "";
		}
		return sReturn;
	}

	public int getiOrderNr() 
	{
		return iOrderNr;
	}

	public void setiOrderNr(int iOrderNr) 
	{
		this.iOrderNr = iOrderNr;
	}

	public int getiCustomerNr() 
	{
		return iCustomerNr;
	}

	public void setiCustomerNr(int iCustomerNr) 
	{
		this.iCustomerNr = iCustomerNr;
	}

	public String getsComment() 
	{
		return sComment;
	}

	public void setsComment(String sComment) 
	{
		this.sComment = sComment;
	}

	public int getiAdressNr() 
	{
		return iAdressNr;
	}

	public void setiAdressNr(int iAdressNr) 
	{
		this.iAdressNr = iAdressNr;
	}

	public int getiStatusNr() 
	{
		return iStatusNr;
	}

	public void setiStatusNr(int iStatusNr) 
	{
		this.iStatusNr = iStatusNr;
	}
	
	
}
